package ar.com.gep.soccer.frontend.controllers;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.jboss.logging.Logger;

import ar.com.cdt.framework.business.exception.CDTFrameworkBusinessException;
import ar.com.gep.soccer.frontend.lazydatamodels.BaseLazyDataModel;
import ar.com.gep.soccer.frontend.lazydatamodels.TeamLazyDataModel;
import ar.com.gep.soccer.frontend.utils.FacesUtil;
import ar.com.gep.soccer.services.TeamService;
import ar.com.gep.soccer.services.dto.TeamDTO;

@ManagedBean(name = "teamMB")
@ViewScoped
public class TeamManageBean extends BaseManageBean {

	private static final Logger log = Logger.getLogger(TeamManageBean.class);

	@ManagedProperty(value = "#{teamServiceImpl}")
	protected TeamService teamService;
	protected TeamDTO teamDTO;
	protected BaseLazyDataModel<TeamDTO> teamLazyDataModel;

	@PostConstruct
	public void init() {
		Long teamId = (Long) FacesUtil.getParametersFlash(KEY_TEAM);

		if (teamId != null) {
			try {
				teamDTO = teamService.getById(teamId);
			} catch (CDTFrameworkBusinessException e) {
				log.errorv("TeamManageBean - init(): Ocurrio un error. Causa: {1}", e);
			}
		} else {
			teamDTO = new TeamDTO();
		}
	}

	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}

	public TeamDTO getTeamDTO() {
		return teamDTO;
	}

	public void setTeamDTO(TeamDTO teamDTO) {
		this.teamDTO = teamDTO;
	}

	/**
	 * 
	 * @return
	 */
	public BaseLazyDataModel<TeamDTO> getTeamLazyDataModel() {
		if (teamLazyDataModel == null) {
			teamLazyDataModel = new TeamLazyDataModel(teamService);
		}
		return teamLazyDataModel;
	}

	public void setTeamLazyDataModel(BaseLazyDataModel<TeamDTO> teamLazyDataModel) {
		this.teamLazyDataModel = teamLazyDataModel;
	}

	/**
	 * Carga la pantalla inicial del módulo
	 * 
	 * @return ResultAction
	 */
	public String viewDetail(final Long teamId) {
		log.infov("ManageBean Task - viewDetail(): redirigiendo a la pantalla de details");
		FacesUtil.addParametersFlash(KEY_TEAM, teamId);

		return VIEW_ACTION;
	}

	/**
	 * @return String
	 */
	public String create() {
		return SUCCESS_ACTION;
	}

	// public String edit(CategoryDTO categoryDTO) {
	// FacesUtil.addParametersFlash(KEY_CATEGORY, categoryDTO.getId());
	// return EDIT_ACTION;
	// }

	// public String save() {
	// log.infov("ManageBean {0}(): Almacenando una categoria.", "save");
	// try {
	// getCategoryService().saveEntity(categoryDTO);
	// return SUCCESS_ACTION;
	// } catch (CDTBagoraBusinessException e) {
	// log.errorv("ManageBean {0}(): Se produjo un error al almacenar una categoria.", "save");
	// return UNSUCCESS_ACTION;
	// }
	// }

	// public String update() {
	// log.infov("ManageBean {0}(): Actualizando una categoria.", "update");
	// try {
	// getCategoryService().updateCategory(categoryDTO);
	// return SUCCESS_ACTION;
	// } catch (CDTBagoraBusinessException e) {
	// log.errorv("ManageBean {0}(): Se produjo un error al almacenar una categoria.", "update");
	// return UNSUCCESS_ACTION;
	// }
	// }

	// public void delete(Long idCategory) {
	// log.infov("ManageBean {0}(): Eliminando una categoria una categoria.", "delete");
	// try {
	// categoryService.deleteById(idCategory);
	// FacesUtil.showInfoMessage("messages", "successDelete");
	// } catch (CDTBagoraBusinessException e) {
	// log.errorv("ManageBean {0}(): Se produjo un error al eliminar una categoria.", "delete");
	// FacesUtil.showErrorMessage("messages", "constraintDelete");
	// }
	// }

	// /**
	// * Verifica que el nombre de la categoria no exista en la base
	// */
	// public void validateName(FacesContext context, UIComponent component, Object convertedValue) {
	// log.infov("ManageBean - {0}(): Verificando si el nombre de la cateogira ya existe", "validateName");
	// CategoryDTO filterDTO = new CategoryDTO();
	// filterDTO.setName((String) convertedValue);
	// List<CategoryDTO> categoryDTOs;
	// try {
	// categoryDTOs = categoryService.getCategoryByFilter(filterDTO);
	// if (existsDTO(categoryDTOs, categoryDTO)) {
	// throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "", FacesUtil.getMessage("uniqueName")));
	// }
	// } catch (CDTBagoraBusinessException exception) {
	// log.errorv("ManageBean - {0}(): Error al verificar si el nombre de la categoria ya existe. Causa: {1}", "validateName", exception);
	// }
	// }

}
